package ds.project.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import ds.project.datamodel.FileInformation;

public class FileUtils {	
	public static void WriteFile(File filePath, OutputStream out) throws java.io.IOException{	        
        FileInputStream reader = new FileInputStream(filePath);                
        byte[] buf = new byte[1024];
        int numRead=0;        
        while((numRead=reader.read(buf))!= -1){
            out.write(buf,0,numRead);
            buf = new byte[1024];
        }
        reader.close();	        
	}
	
	public static void SaveStringAsFile(String filePath, InputStream in) throws java.io.IOException{		
		OutputStream out = new FileOutputStream(filePath);
        byte[] buf = new byte[1024];
        int numRead=0;
        try
        {        	
        	while((numRead=in.read(buf)) != -1){        	
            	out.write(buf,0,numRead);
                buf = new byte[1024];
            }
        }
        catch (IOException e)
        {
        	out.close();
        	throw new IOException();        	
        }
		out.close();	
	}
		
	public static List<FileInformation> GetFilesOnDirectory(File directory)throws java.io.IOException{
		
		List<FileInformation> ret=new ArrayList<FileInformation>();
		for(File f:directory.listFiles()){
			if(f.isFile()){
				FileInformation fileInformation = new FileInformation();
				fileInformation.setSize(f.length());
				fileInformation.setName(f.getName());
				ret.add(fileInformation);
			}
		}
		return ret;
	}
}
